﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PhotoUI : MonoBehaviour {

    public RawImage img;
	void Start () 
    {
        //注册照片事件
        MessageManager.RegisterMessage(MessageName.PhotoLoaded, OnPlotonLoad);
	}

    
    void OnDestroy()
    {
        //移除事件
        MessageManager.RemoveMessage(MessageName.PhotoLoaded, OnPlotonLoad);
    }

    public void TakePhoto()
    {
        PhotoPluginManager.TakePhoto();
    }

    public void LocalPhoto()
    {
        PhotoPluginManager.LocalPhoto();
    }

    
    void OnPlotonLoad(object obj)
    {
        StartCoroutine(ReloadImg(obj.ToString()));
    }

    /// <summary>
    /// 本地读取图片
    /// </summary>
    /// <param name="imgPath"></param>
    /// <returns></returns>
    IEnumerator ReloadImg(string imgPath)
    {
        string path = "file:///" + Application.persistentDataPath + "/" + imgPath;
        WWW www = new WWW(path);
        yield return www;
        Debug.Log(www.error);
        Texture t = www.texture;
        img.texture = t;
    }
}
